﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BuildHelper.Work.Tools
{
    public partial class ErrorsPanel : UserControl
    {
        public ErrorsPanel()
        {
            InitializeComponent();
        }
        List<Error> errors = new List<Error>();
        public Entry ErrorEntry
        {
            get;
            set;
        }

        public void ClearErrors()
        {
            errors.Clear();
        }
        const string GuiErrorMask = "(\\w:[^\\(]*)\\((\\d*),\\s*(\\d*)\\):(.*)";
        const string JavaErrorMask = "Error in (.*?) \\(at line (\\d*)\\) (.*)$";
        public void AddError(List<string> errors)
        {
            foreach (string s in errors)
                AddError(s);
        }
        public void AddError(string error)
        {
            if (ErrorEntry == Entry.Gui)
            {
                Dictionary<int, string> err = error.GetFirstMatch(GuiErrorMask, new int[] { 1, 2, 3, 4 });
                if (err != null && err[1].IsNotNull() &&
                    err[2].IsNotNull() && err[4].IsNotNull())
                {
                    errors.Add(new Error { Plik = err[1], Linia = err[2].AsInt(), Kolumna = err[3].AsInt(), Blad = err[4] });
                }
            }
            else if (ErrorEntry == Entry.Java)
            {
                Dictionary<int, string> err = error.GetFirstMatch(JavaErrorMask, new int[] { 1, 2, 3 });
                if (err != null && err[1].IsNotNull() &&
                    err[2].IsNotNull() && err[3].IsNotNull())
                {
                    errors.Add(new Error { Plik = err[1], Linia = err[2].AsInt(), Blad = err[3] });
                }
            }
        }

        private void btnErrors_Click(object sender, EventArgs e)
        {
            if (errors.Count > 0)
            {
                using (FormErrors f = new FormErrors(errors))
                {
                    f.ErrorEntry = this.ErrorEntry;
                    f.ShowDialog();
                }
            }
            else
            {
                this.FindForm().ShowDxInfoDialog("Brak błędów do pokazania.");
            }
        }

        private void btnFirstErr_Click(object sender, EventArgs e)
        {
            if (errors.Count > 0)
            {
                FormErrors.OpenError(errors[0], ErrorEntry);
            }
            else
            {
                this.FindForm().ShowDxInfoDialog("Brak błędów do pokazania.");
            }
        }

        public SimpleButton BtnErrors
        {
            get { return btnErrors; }
        }
        public SimpleButton BtnFirstError
        {
            get { return btnFirstErr; }
        }
        public SimpleButton BtnLogFirst
        {
            get { return btnLogFirst; }
        }
        public SimpleButton BtnBalmeFirst
        {
            get { return btnBlameFirstError; }
        }
        private void btnLogFirst_Click(object sender, EventArgs e)
        {
            if (errors.Count > 0)
            {
                GlobalTools.TortoiseTools.ShowTortoiseLogForFile(errors[0].Plik);
            }
            else
            {
                this.FindForm().ShowDxInfoDialog("Brak błędów do pokazania.");
            }
        }

        private void btnBlameFirstError_Click(object sender, EventArgs e)
        {
            if (errors.Count > 0)
            {
                GlobalTools.TortoiseTools.ShowTortoiseBlameForFile(errors[0].Plik);
            }
            else
            {
                this.FindForm().ShowDxInfoDialog("Brak błędów do pokazania.");
            }
        }
    }
}
